/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.stocktransfer;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.stocktransfer.service.SubStockTransferDomain;
import com.lightkits.wms.dto.stocktransfer.SubStockTransferRequestType;
import com.lightkits.wms.dto.stocktransfer.SubStockTransferResponseType;
import com.lightkits.wms.dto.stocktransfer.model.SubStockViewDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 子库存转移服务
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/17 15:25
 */
@Component
public class SubStockTransferExecutor implements
        ServiceExecutor<SubStockTransferRequestType, SubStockTransferResponseType> {
    private final SubStockTransferDomain subStockTransferDomain;

    @Autowired
    public SubStockTransferExecutor(SubStockTransferDomain subStockTransferDomain) {
        this.subStockTransferDomain = subStockTransferDomain;
    }

    /**
     * 子库存转移服务
     *
     * @param request 子库存转移参数
     * @return
     **/
    @Override
    public SubStockTransferResponseType service(SubStockTransferRequestType request) {
        SubStockTransferResponseType response = new SubStockTransferResponseType();
        response.setSuccessful(true);
        SubStockViewDTO viewDTO = request.getSubStock();
        try {
            IResult result = subStockTransferDomain.subStockTransfer(viewDTO);
            if (!result.isSuccessful()) {
                return result.fill(response);
            }
            result.fill(response);
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            response.setSuccessful(false);
            response.setMessage("Save SubStocker Transfer Quantity error");
        }
        return response;
    }
}
